package com.dawn.module.hospital.service;

import com.dawn.module.hospital.enums.BHConstants;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.infra.api.config.ConfigApi;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 15:50
 */
public abstract class BHService {


    @Autowired
    protected ConfigApi configApi;

    private String hisServerUri;

    private String callServerUri;

    private String hisWxChsServerUri;

    private String hisAliChsServerUri;

    private EhealthCfg ehealthCfg;

    private TemplateMsgCfg templateMsgCfg;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Data
    class EhealthCfg {

        private String hospitalId;

        private String appId;

        private String appSecret;

        public EhealthCfg() {
        }

        public EhealthCfg(String hospitalId, String appId, String appSecret) {
            this.hospitalId = hospitalId;
            this.appId = appId;
            this.appSecret = appSecret;
        }
    }

    @Data
    class TemplateMsgCfg {
        private String appId;

        private String rootUrl;

        private String templateMsgIdOutpatient;

        private String templateMsgIdRegister;

        private String templateMsgIdRegisterCancel;

        private String templateMsgIdPatient;

        private String templateMsgIdInHospital;

        private String templateMsgIdRefund;

        private String templateMsgIdRefundApply;

        private String templateMsgIdNotify;

        private String templateMsgIdMedicalApply;

        private String templateMsgIdMedicalExamineSuccess;

        private String templateMsgIdMedicalExamineFails;

        private String templateMsgIdMedicalPaySuccess;

        private String templateMsgIdMedicalDelivery;

        private String templateMsgIdMedicalTake;


        public TemplateMsgCfg() {
        }

        public TemplateMsgCfg(String appId,String rootUrl,String templateMsgIdOutpatient, String templateMsgIdRegister,
                              String templateMsgIdRegisterCancel, String templateMsgIdPatient, String templateMsgIdInHospital,
                              String templateMsgIdRefund, String templateMsgIdRefundApply, String templateMsgIdNotify,
                              String templateMsgIdMedicalApply, String templateMsgIdMedicalExamineSuccess, String templateMsgIdMedicalExamineFails,
                              String templateMsgIdMedicalPaySuccess, String templateMsgIdMedicalDelivery, String templateMsgIdMedicalTake) {
            this.appId = appId;
            this.rootUrl = rootUrl;
            this.templateMsgIdOutpatient = templateMsgIdOutpatient;
            this.templateMsgIdRegister = templateMsgIdRegister;
            this.templateMsgIdRegisterCancel = templateMsgIdRegisterCancel;
            this.templateMsgIdPatient = templateMsgIdPatient;
            this.templateMsgIdInHospital = templateMsgIdInHospital;
            this.templateMsgIdRefund = templateMsgIdRefund;
            this.templateMsgIdRefundApply = templateMsgIdRefundApply;
            this.templateMsgIdNotify = templateMsgIdNotify;

            this.templateMsgIdMedicalApply = templateMsgIdMedicalApply;
            this.templateMsgIdMedicalExamineSuccess = templateMsgIdMedicalExamineSuccess;
            this.templateMsgIdMedicalExamineFails = templateMsgIdMedicalExamineFails;
            this.templateMsgIdMedicalPaySuccess = templateMsgIdMedicalPaySuccess;
            this.templateMsgIdMedicalDelivery = templateMsgIdMedicalDelivery;
            this.templateMsgIdMedicalTake = templateMsgIdMedicalTake;
        }
    }

    protected static final Integer ERROR_CODE = ErrorCodeConstants.BH_ERROR.getCode();

//    protected String getHisServerUri() {
//        if (hisServerUri == null) {
//            hisServerUri = configApi.getConfigValueByKey(BHConstants.HIS_SERVER_URI.getValue());
//        }
//        return hisServerUri;
//    }

    protected String getHisServerUri() {
        String redisKey = BHConstants.HIS_SERVER_URI.getValue();
        String uri = stringRedisTemplate.opsForValue().get(redisKey);
        if (uri == null) {
            uri = configApi.getConfigValueByKey(BHConstants.HIS_SERVER_URI.getValue());
            if(StringUtils.isNotBlank(uri)){
                stringRedisTemplate.opsForValue().set(redisKey, uri, 24, TimeUnit.HOURS);
            }
        }
        return uri;
    }

    protected String getCallServerUri() {
        if (callServerUri == null) {
            callServerUri = configApi.getConfigValueByKey(BHConstants.CALL_SERVER_URI.getValue());
        }
        return callServerUri;
    }

    protected String getEInvoiceOpenUri() {
        String redisKey = BHConstants.INVOICE_OPEN_SERVER_URI.getValue();
        String uri = stringRedisTemplate.opsForValue().get(redisKey);
        if (uri == null) {
            uri = configApi.getConfigValueByKey(redisKey);
            if(StringUtils.isNotBlank(uri)){
                stringRedisTemplate.opsForValue().set(redisKey, uri, 24, TimeUnit.HOURS);
            }
        }
        return uri;
    }

    protected String getEInvoiceCancelUri() {
        String redisKey = BHConstants.INVOICE_CANCEL_SERVER_URI.getValue();
        String uri = stringRedisTemplate.opsForValue().get(redisKey);
        if (uri == null) {
            uri = configApi.getConfigValueByKey(redisKey);
            if(StringUtils.isNotBlank(uri)){
                stringRedisTemplate.opsForValue().set(redisKey, uri, 24, TimeUnit.HOURS);
            }
        }
        return uri;
    }

    protected String getEInvoiceOperator() {
        String redisKey = BHConstants.INVOICE_OPERATOR.getValue();
        String v = stringRedisTemplate.opsForValue().get(redisKey);
        if (v == null) {
            v = configApi.getConfigValueByKey(redisKey);
            if(StringUtils.isNotBlank(v)){
                stringRedisTemplate.opsForValue().set(redisKey, v, 24, TimeUnit.HOURS);
            }
        }
        return v;
    }

    protected String getHisWxChsServerUri() {
        if (hisWxChsServerUri == null) {
            hisWxChsServerUri = configApi.getConfigValueByKey(BHConstants.HIS_WX_CHS_SERVER_URI.getValue());
        }
        return hisWxChsServerUri;
    }

    protected String getHisAliChsServerUri() {
        if (hisAliChsServerUri == null) {
            hisAliChsServerUri = configApi.getConfigValueByKey(BHConstants.HIS_ALI_CHS_SERVER_URI.getValue());
        }
        return hisAliChsServerUri;
    }

    protected EhealthCfg getEhealthCfg() {
        if (ehealthCfg == null) {
            String hospitalId = configApi.getConfigValueByKey(BHConstants.EHEALTH_HOSPITAL_ID.getValue());
            if (StringUtils.isNotBlank(hospitalId)) {
                String appId = configApi.getConfigValueByKey(BHConstants.EHEALTH_APP_ID.getValue());
                if (StringUtils.isNotBlank(appId)) {
                    String appSecret = configApi.getConfigValueByKey(BHConstants.EHEALTH_APP_SECRET.getValue());
                    if (StringUtils.isNotBlank(appSecret)) {
                        ehealthCfg = new EhealthCfg(hospitalId, appId, appSecret);
                    }
                }
            }
        }
        return ehealthCfg;
    }

    protected TemplateMsgCfg getTemplateMsgCfg() {
        String redisKey = BHConstants.MSG_APPID.getValue();
        String appId = stringRedisTemplate.opsForValue().get(redisKey);

        if (templateMsgCfg == null || appId == null) {
            appId = configApi.getConfigValueByKey(BHConstants.MSG_APPID.getValue());
            if(StringUtils.isNotBlank(appId)){
                stringRedisTemplate.opsForValue().set(redisKey, appId, 24, TimeUnit.HOURS);
            }

            String rootUrl = configApi.getConfigValueByKey(BHConstants.MSG_ROOT_URL.getValue());
            String templateMsgIdOutpatient = configApi.getConfigValueByKey(BHConstants.MSG_ID_OUTPATIENT.getValue());
            String templateMsgIdRegister = configApi.getConfigValueByKey(BHConstants.MSG_ID_REGISTER.getValue());
            String templateMsgIdRegisterCancel = configApi.getConfigValueByKey(BHConstants.MSG_ID_REGISTER_CANCEL.getValue());
            String templateMsgIdPatient = configApi.getConfigValueByKey(BHConstants.MSG_ID_PATIENT.getValue());
            String templateMsgIdInHospital = configApi.getConfigValueByKey(BHConstants.MSG_ID_IN_HOSPITAL.getValue());
            String templateMsgIdRefund = configApi.getConfigValueByKey(BHConstants.MSG_ID_REFUND.getValue());
            String templateMsgIdRefundApply = configApi.getConfigValueByKey(BHConstants.MSG_ID_REFUND_APPLY.getValue());
            String templateMsgIdNotify = configApi.getConfigValueByKey(BHConstants.MSG_ID_NOTIFY.getValue());

            String templateMsgIdMedicalApply = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_APPLY.getValue());
            String templateMsgIdMedicalExamineSuccess = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_EXAMINE_SUCCESS.getValue());
            String templateMsgIdMedicalExamineFails = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_EXAMINE_FAILS.getValue());
            String templateMsgIdMedicalPaySuccess = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_PAY_SUCCESS.getValue());
            String templateMsgIdMedicalDelivery = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_DELIVERY.getValue());
            String templateMsgIdMedicalTake = configApi.getConfigValueByKey(BHConstants.MSG_ID_MEDICAL_TAKE.getValue());
            templateMsgCfg = new TemplateMsgCfg(appId,rootUrl,templateMsgIdOutpatient,templateMsgIdRegister,templateMsgIdRegisterCancel,templateMsgIdPatient,templateMsgIdInHospital,
                    templateMsgIdRefund,templateMsgIdRefundApply,templateMsgIdNotify,templateMsgIdMedicalApply,templateMsgIdMedicalExamineSuccess,templateMsgIdMedicalExamineFails,templateMsgIdMedicalPaySuccess,templateMsgIdMedicalDelivery,templateMsgIdMedicalTake);
        }
        return templateMsgCfg;
    }

}
